<?php
require_once "ConsistentHashing.php";

$hash = new Flexihash();

$number = 100;//测试数量

$hash->addTarget("node1");

$hash->addTarget("node2");

$hash->addTarget("node3");

$hash->addTarget("node4");

$oneCount = array();
for($i=1;$i<=$number;$i++){
	$node = $hash->lookup($i);
	if(isset($oneCount[$node])){
		$oneCount[$node]['count']++;
		$oneCount[$node]['contents'] .= ','.$i;
	}else{
		$oneCount[$node]['count'] = 1;
		$oneCount[$node]['contents'] = $i;
	}
}


$hash->addTarget("node5");
$hash->addTarget("node6");
$twoCount = array();
for($i=1;$i<=$number;$i++){
	$node = $hash->lookup($i);
	if(isset($twoCount[$node])){
		$twoCount[$node]['count']++;
		$twoCount[$node]['contents'] .= ','.$i;
	}else{
		$twoCount[$node]['count'] = 1;
		$twoCount[$node]['contents'] = $i;
	}
}

echo "<pre>";
ksort($oneCount);
var_export($oneCount);
echo "<br/>";
ksort($twoCount);
var_export($twoCount);
echo "</pre>";

/**
 * 一致性哈希可以做到在增减节点的时候影响最小的数据而达到均衡
 */
